/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aso.upna.rmi;

import aso.upna.playlist.GenericList;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author triki
 */
public class ImpProcInterface extends UnicastRemoteObject
                              implements ProcInterface
{

    private String path_base;

    public ImpProcInterface(String path) throws RemoteException
    {
        super();
        path_base = new String(path);
    }

    public GenericList getCanciones()
    {
        GenericList lista = new GenericList();
        String aux[] = null;
        FileInputStream fi = null;
        ObjectInputStream oi = null;
        File g = null;
        
        int i;

        File f = new File(path_base);

        if(!f.exists())
            return null;

        aux = f.list();
        try
        {
            for(i=0;i<aux.length;i++)
            {
                g = new File(path_base+aux[i]);
                fi = new FileInputStream(g);
                oi = new ObjectInputStream(fi);
                Object obj = oi.readObject();

                if(obj instanceof GenericList)
                {
                    lista = GenericList.mergeLists(lista, (GenericList)obj);
                }
            }
            //Una vez leido, eliminamos el fichero para que no vuelva
            //a ser leido en posteriores iteraciones.
            g.delete();
            
            //devolvemos una lista con todos los ficheros compartidos.
            //una unica lista para evitar problemas.
            return lista;
        }
        catch (ClassNotFoundException ex) {return null;}
        catch(IOException ex){return null;}
    }

}
